/**
 * JAVASCRIPT
 *
 * @author        qiuminggang
 * @date          2012/05/18
 * @version       1.0
 * @description   流量统计
 * @history       <author><time><version><desc>
 */

(function(){
	var d = document,w = window, n = navigator, l = location, s = screen, ec = encodeURIComponent, dc = decodeURIComponent, m = function(){
		this.className = "STATISTICS";
		this.NR = "/getData";
		this.init();
	};
	
	m.prototype = {
		init:function(){
			this.date = new Date;
			this.now = parseInt(this.date.getTime());
			this.date.setTime(this.now);
		},
		getAll : function() {
			this.getDomain();
			this.getReferer();
			this.getPath();
			this.getPageName();
			this.getSys();
			this.getBrowser();
			this.getLG();
			this.getScreenRes();
			this.gePlugins();
			this.getFlashVersion();
			this.getIP();
		},
		getReferer : function() {
			this.refer = d.referrer || "";
			//this.refer = this.refer;
		},
		getDomain : function() {
			this.domain = d.domain || "";
			//this.refer = this.refer;
		},
		getPath : function() {
			//this.path = l.pathname;
			this.path = d.URL;
		},
		getPageName : function(){
			this.href = l.href;
			this.pageName = this.href.slice(this.href.lastIndexOf("/")+1);
		},
		getSys : function(){
			var sUserAgent = navigator.userAgent;
			//检测操作系统
			var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
			var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh");

			var isUnix = (navigator.platform == "X11" && !isWin && !isMac);

			var isWin95 = isWin98 = isWinNT4 = isWin2K = isWinME = isWinXP = isWin2K3 = isVista = false;
			var isMac68K = isMacPPC = false;
			var isSunOS = isMinSunOS4 = isMinSunOS5 = isMinSunOS5_5 = false;
			if(isWin){
				isWin95 = sUserAgent.match("Win95") || sUserAgent.match("Windows 95");
			    isWin98 = sUserAgent.match("Win98") || sUserAgent.match("Windows 98");
			    isWinME = sUserAgent.match("Win 9x 4.90") || sUserAgent.match("Windows ME");
			    isWin2K = sUserAgent.match("Windows NT 5.0") || sUserAgent.match("Windows 2000");
			    isWinXP = sUserAgent.match("Windows NT 5.1") || sUserAgent.match("Windows XP");
			    isWin2K3 = sUserAgent.match("Windows NT 5.2") || sUserAgent.match("Windows 2003");
			    //检测Vista系统可能有bug
			    isVista = sUserAgent.match("Windows NT 6.0") || sUserAgent.match("Windows Vista");
			    if(!isWin2K3&&!isVista){
			        isWinNT4 = sUserAgent.match("WinNT")
			                    || sUserAgent.match("Windows NT")
			                    || sUserAgent.match("WinNT4.0")
			                    || (sUserAgent.match("Windows NT 4.0"))
			                    && (!isWinME&&!isWin2K&&!isWinXP);
			    }
			    var arr = [isWin95, isWin98, isWinME, isWin2K, isWinXP, isWin2K3, isVista, isWinNT4];
			    for(var i = 0;i < arr.length;i++){
			    	if (arr[i]) this.sys = arr[i];
			    }
			};
			
			if(isMac){
			    isMac68K = sUserAgent.match("Mac_68000") || sUserAgent.match("68K");
			    isMacPPC = suserAgent.match("MacPowerPC") || sUserAgent.match("PPC");
			    var arr2 = [isMac68K, isMacPPC];
			    for(var ii = 0;ii < arr2.length;ii++){
			    	if (arr2[ii]) this.sys = arr2[ii];
			    }
			};
			
			if(isUnix) this.sys = "Unix";
		},
		getBrowser : function() {
			var fAppVersion = parseFloat(navigator.appVersion);
			//比较浏览器版本大小
			function compareVersions(sVersion1,sVersion2){
			    var aVersion1 = sVersion1.split(".");
			    var aVersion2 = sVersion2.split(".");
			
			    if(aVersion1.length>aVersion2.length){
			        for(var i=0;i<aVersion1.length;i++)
			            aVersion2.push("0");
			    }else if(aVersion1.length<aVersion2.length){
			        for(var i=0;i<aVersion2.length;i++)
			            aVersion1.push("0");
			    }
			
			    for(var i=0;i<aVersion1.length;i++){
			        if(aVersion1[i]<aVersion2[i])
			            return -1;
			        else if(aVersion1[i]>aVersion2[i])
			            return 1;
			    }
			    return 0;
			}
			//检测是否Opera浏览器
			var sUserAgent = navigator.userAgent;
			var isOpera = sUserAgent.indexOf("Opera")>-1;
			var isMinOpera4 = isMinOpera5 = isMinOpera6 = isMinOpera7 = isMinOpera7_5 = false;
			
			if(isOpera){
			    var fOperaVersion;
			    if(navigator.appName == "Opera"){
			        fOperaVersion = fAppVersion;
			    } else {
			        var reOperaVersion = new RegExp("Opera (\d+\.\d+)");
			        reOperaVersion.test(sUserAgent);
			        fOperaVersion = parseFloat(RegExp["$1"]);
			    }
			
			    this.browser = "Opera" + fOperaVersion;
			}
			//检测是否Safari
			var isKHTML = sUserAgent.indexOf("KHTML")>-1
			                || sUserAgent.indexOf("Konqueror")>-1
			                || sUserAgent.indexOf("AppleWebKit")>-1;
			
			var isMinSafari1 = isMinSafari1_2 = false;
			var isMinKonq2_2 = isMinKonq3 = isMinKonq3_1 = isMinKonq3_2 = false;
			if(isKHTML){
			    isSafari = sUserAgent.indexOf("AppleWebKit")>-1;
			    isKonq = sUserAgent.indexOf("Konqueror")>-1;
			    if(isSafari){
			        var isChrome = sUserAgent.indexOf("Chrome")>-1;
			        this.browser = isChrome? "Chrome":"Safari";
			    } else if (isKonq) {
			        this.browser = "Konq";
			    }
			}
			//检测IE
			var isIE = sUserAgent.indexOf("compatible") > -1
			            &&sUserAgent.indexOf("MSIE") > -1
			            &&!isOpera;
			
			var isMinIE4 = isMinIE5 = isMinIE5_5 = isMinIE6 = isMinIE7 = false;
			if(isIE){
			    /MSIE (\d+\.\d+);/.test(sUserAgent);
			    var fIEVersion = parseFloat(RegExp["$1"]);
			    
			    this.browser = "IE" + fIEVersion;
			}
			//检测Mozilla
			var isMoz = sUserAgent.indexOf("Gecko")>-1&&!isKHTML;
			var isMinMoz1 = isMinMoz1_4 = isMinMoz1_5 = isMinMoz1_8 = false;
			if(isMoz){
				//检测Firfox
				var isFF = sUserAgent.indexOf("Firefox")>-1&&!isKHTML;
				var isMinFF1 = isMinFF2 =isMinFF3 = false;
				if(isFF){
				    /Firefox\/(\d+\.\d+)/.test(sUserAgent);
				    var fffVersion = parseFloat(RegExp["$1"]);
				    this.browser = "Firefox" + fffVersion;
				}else{
					/rv:(\d+\.\d+)/.test(sUserAgent);
			    	var fMOZVersion = parseFloat(RegExp["$1"]);
			    	this.browser = "Mozilla" + fMOZVersion;
				} 
			}
			var isNS4 = !isIE&&!isOpera&&!isMoz&&!isKHTML
			            &&(sUserAgent.indexOf("Mozilla")==0)
			            &&(navigator.appName == "Netscape")
			            &&(fAppVersion >= 4.0 && fAppVersion <5.0);
			var isMinNS4 = isMinNS4_5 = isMinNS4_7 =isMinNS4_8 = false;
			
			if(isNS4){
			    isMinNS4 = true;
			    this.browser = "Netscape" + fAppVersion;
			}
		},
		getLG : function() {
			this.lg = n.systemLanguage || n.language;
			this.lg = this.lg.toLowerCase();
		},
		getScreenRes : function(){
			this.px = screen.pixelDepth;
			this.screenRes = screen.width + '×' + screen.height;
		},
		gePlugins : function(){
			//插件数目
			this.pluginsNum = n.plugins.length;
			
			var arr = [];
			
			for(var ii = 0;ii < this.pluginsNum;ii++){
				var name = n.plugins[ii].name;
				var fileName = n.plugins[ii].filename;
				arr.push(name + ":" + fileName);
			}
			
			this.plugins = arr.join(",");
		},
		getFlashVersion : function() {
			var f = "-";
			if(n.plugins && n.plugins.length){
				for(var ii = 0;ii < n.plugins.length;ii++){
					if(n.plugins[ii].name.indexOf('Shockwave Flash')!=-1){
						f = n.plugins[ii].description.split('Shockwave Flash')[1];
						break;
					}
				}
			}else if(w.ActiveXObject){
				for(var ii = 10;ii >= 2;ii--){
					try{
						var fl = eval("new ActiveXObject('ShockwaveFlah.ShockwaveFlash." + ii + "');");
						if(fl){
							f = ii + '.0';
							break;
						}
					}catch(e){
						f = "";
					}
				}
			}
			this.flash = f;
		},
		getIP : function(){
			$.ajax({
				url:"http://whois.pconline.com.cn/ipJson.jsp",
				dataType:"script",
				data:{callback: 'ygy.ipaddress'},
				success:function(data){
				}

			});

		},
		
		ipaddress:function(json){
			this.soip=json.ip;
			this.addr=json.addr;
			this.city=json.city;
			this.pro=json.pro;

		},
		requestNext : function() {
			
			this.getAll();
			var jsonData = {
				'time' : this.now,
				'refer' : escape(this.refer),
				'domain':escape(this.domain),
				'path' : escape(this.path),
				'page' : escape(this.pageName),
				'os' : escape(this.sys),
				'browser' : this.browser,
				'lg' : this.lg,
				'screenres' : this.screenRes,
				'plugins' : this.plugins,
				'flash' : this.flash,
				'firVisit' : this.firVisit,
				'ip' : this.soip,
				'ipaddr':this.addr,
				'ipcity':this.city,
				'ippro':this.pro

			};
			//alert(jsonData);
			var a = this.NR + "?data=" + JSON.stringify(jsonData);
			//alert(a);
			document.write("<img src='" + a + "' width=0 height=0 border=0 />");
		}

	};
	
	var g = new m();
	window.ygy=g;
	w[g.className] = g;
	
	try{
		STATISTICS.requestNext();
	}catch(error){
		console.error(error.message);
	}
	
})();